package co.chuangdian.oflauncher.widgets.setting;

import co.chuangdian.oflauncher.OFActivity;
import co.chuangdian.oflauncher.R;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

@SuppressWarnings("deprecation")
public class HomeActivity extends OFActivity implements ActionBar.TabListener {
	private ManageFragment manageFragment = new ManageFragment();
	private SortFragment sortFragment = new SortFragment();

	private static final int TAB_INDEX_COUNT = 2;
	private static final int TAB_INDEX_ONE = 0;
	private static final int TAB_INDEX_TWO = 1;

	private ViewPager mViewPager;
	private ViewPagerAdapter mViewPagerAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.widgetsetting);

		//setUpActionBar();
		// setUpViewPager();
		// setUpTabs();
	}

	private void setUpActionBar() {
		final ActionBar actionBar = getActionBar();
		actionBar.setHomeButtonEnabled(false);
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		actionBar.setDisplayShowHomeEnabled(false);
	}

	private void setUpViewPager() {
		mViewPagerAdapter = new ViewPagerAdapter(this.getSupportFragmentManager());

		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mViewPagerAdapter);
		mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				final ActionBar actionBar = getActionBar();
				actionBar.setSelectedNavigationItem(position);
			}

			@Override
			public void onPageScrollStateChanged(int state) {
				switch (state) {
				case ViewPager.SCROLL_STATE_IDLE:
					break;
				case ViewPager.SCROLL_STATE_DRAGGING:
					break;
				default:
					break;
				}
			}
		});
	}

	private void setUpTabs() {
		final ActionBar actionBar = getActionBar();
		for (int i = 0; i < mViewPagerAdapter.getCount(); i++) {
			actionBar.addTab(actionBar.newTab().setText(mViewPagerAdapter.getPageTitle(i)).setTabListener(this));
		}
	}

	@Override
	protected void onDestroy() {

		super.onDestroy();
	}

	public class ViewPagerAdapter extends FragmentPagerAdapter {

		public ViewPagerAdapter(FragmentManager fm) {
			super(fm);

		}

		@Override
		public Fragment getItem(int position) {
			switch (position) {
			case TAB_INDEX_ONE:
				return manageFragment;

			case TAB_INDEX_TWO:
				return sortFragment;
			}
			throw new IllegalStateException("No fragment at position " + position);
		}

		@Override
		public int getCount() {

			return TAB_INDEX_COUNT;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			String tabLable = null;
			switch (position) {
			case TAB_INDEX_ONE:
				tabLable = getString(R.string.widgetsetting_manage);
				break;
			case TAB_INDEX_TWO:
				tabLable = getString(R.string.widgetsetting_sort);
				break;
			}
			return tabLable;
		}

	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		mViewPager.setCurrentItem(tab.getPosition());
	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {

	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {

	}

}
